package com.songlanyun.modules.servicegoods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.songlanyun.common.validator.ValidatorUtils;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.Query;

import com.songlanyun.modules.servicegoods.dao.ServiceGoodsContentDao;
import com.songlanyun.modules.servicegoods.entity.ServiceGoodsContent;
import com.songlanyun.modules.servicegoods.service.ServiceGoodsContentService;


@Service("serviceGoodsContentService")
public class ServiceGoodsContentServiceImpl extends ServiceImpl<ServiceGoodsContentDao, ServiceGoodsContent> implements ServiceGoodsContentService {

    @Override
    public ServiceGoodsContent create(ServiceGoodsContent entity) {
        ValidatorUtils.validateEntity(entity);
        this.baseMapper.insert(entity);
        return entity;
    }

    @Override
    public ServiceGoodsContent modify(ServiceGoodsContent entity) {
        ValidatorUtils.validateEntity(entity);
        this.baseMapper.updateById(entity);
        return entity;
    }

    @Override
    public void remove(Long id) {
        this.removeById(id);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ServiceGoodsContent> page = this.page(
                new Query<ServiceGoodsContent>().getPage(params),
                new QueryWrapper<ServiceGoodsContent>()
        );

        return new PageUtils(page);
    }

    @Override
    public ServiceGoodsContent getByGoodsId(Long id) {
        return this.getOne(new LambdaQueryWrapper<ServiceGoodsContent>().eq(ServiceGoodsContent::getServiceGoodsId,id).last("limit 1"));
    }

    @Override
    public ServiceGoodsContent setContent(Long goodsId, String content) {
        ServiceGoodsContent obj = this.getByGoodsId(goodsId);
        if(obj==null)
        {
            ServiceGoodsContent serviceGoodsContent = new ServiceGoodsContent();
            serviceGoodsContent.setServiceGoodsId(goodsId);
            serviceGoodsContent.setMobileDetail(content);
            return this.create(serviceGoodsContent);
        }
        else
        {
            obj.setMobileDetail(content);
            return this.modify(obj);
        }

    }
}